package com.zyk.scaffold.gateway.filter;

import com.zyk.scaffold.gateway.constant.GatewayConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Component
public class RemoveHeaderFilter implements GlobalFilter, Ordered {
    private static final Logger log = LoggerFactory.getLogger(RemoveHeaderFilter.class);

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        return chain.filter(exchange.mutate().request(exchange.getRequest().mutate().headers(httpHeaders -> {
            httpHeaders.remove(GatewayConstants.OPEN_API_TOKEN_HEADER);
            httpHeaders.remove(GatewayConstants.GATEWAY_FILTER_HEADER_USER_ID);
            httpHeaders.remove(GatewayConstants.GATEWAY_FILTER_HEADER_TENANT_CODE);
            httpHeaders.remove(GatewayConstants.GATEWAY_FILTER_HEADER_JWT_USER_METADATA);
        }).build()).build());
    }

    @Override
    public int getOrder() {
        return Integer.MIN_VALUE + 100;
    }

}
